package com.zhucx;
import com.microsoft.playwright.*;
import com.zhucx.loader.strategy.JsoupPageLoader;
import com.zhucx.loader.strategy.PlaywrightPageLoader;
import com.zhucx.loader.strategy.SeleniumPhantomjsPageLoader;
import com.zhucx.parser.PageParser;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

//下载
public class Examle {

    public static void main(String[] args) {
        ZhuCrawler zhuCrawler = new ZhuCrawler.Builder()
                .setUrls("http://www.12348.gov.cn/#/publicies/lawyerlist/lawyerlist")
//                .setThreadCount(2)
                .setPageLoader(new PlaywrightPageLoader())
                .setAllowSpread(false)
//                .setPageLoader(new SeleniumPhantomjsPageLoader("E:\\git-project\\web_spider\\code\\app\\controllers\\phantomjs\\bin\\phantomjs.exe"))
//                .setSelectElement(".J-lemma-title")
                .setPageParser(new PageParser<Object>(){
                    @Override
                    public void parse(Document html, Element pageVoElement) {
                        // 文件信息
//                        String htmlData = html.html();
                        String pageUrl = html.baseUri();
                        System.out.println(pageUrl + "---：" + html.html());
//                        System.out.println(pageVoElement.select(".title").text());
//                        String fileName = FileUtil.getFileNameByUrl(html.baseUri(), XxlCrawlerConf.CONTENT_TYPE_HTML);
                        // 下载Html文件
//                        FileUtil.saveFile(htmlData, filePath, fileName);
                    }

                })
                .build();

        System.out.println("start");
        zhuCrawler.start(true);
        System.out.println("end");
    }


//    public static void main(String[] args) {
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.chromium().launch();
//            Page page = browser.newPage();
//            page.navigate("https://www.toutiao.com/");
//            System.out.println(page.content());
//        }
//    }

//    public static void main(String[] args) {
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.webkit().launch();
//            Page page = browser.newPage();
//            page.navigate("https://item.jd.com/12228194.html");
//            page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("example.png")));
//        }
//    }

//    public static void main(String[] args) {
//        new Examle().collectionInfo("https://top.baidu.com/board?platform=pc&sa=pcindex_entry");
//    }
//    public void collectionInfo(String spiderUrl){
//        Playwright playwright = Playwright.create();
//        String pageUrl = spiderUrl;
//        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
//        BrowserContext context = browser.newContext();
//        Page page = context.newPage();
//        page.navigate(pageUrl);
//        Locator pzbm = page.locator(".item-wrap_2oCLZ");
//        if (pzbm.count() > 0) {
//            for (int i = 0; i < pzbm.count(); i++) {
//                String text =pzbm.nth(i).innerText();
//                System.out.println(text);
//            }
//
//        }
//
//
//
//    }
public  Boolean tt(){
    ZhuCrawler crawler = new ZhuCrawler.Builder()
            .setUrls("https://machtalk.xiachufang.com/category/40076/")
//                .setThreadCount(3)
            .setSelectElement("#add-more-container")
            .setPageParser(new PageParser<Object>(){
                @Override
                public void parse(Document html, Element pageVoElement) {
                    System.out.println(html.html());
//                        String pageUrl = html.baseUri();
//                        System.out.println(pageUrl + "---：" + html.html());
                }

            })
            .build();

    System.out.println("start");
    crawler.start(true);
    System.out.println("end");
    return true;
}

}
